package com.ken.android.music.view.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v4.media.session.MediaControllerCompat;
import android.support.v4.media.session.PlaybackStateCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.ken.android.music.R;

public class MusicController extends LinearLayout implements View.OnClickListener {

    private ImageView repeatImg;
    private ImageView previousImg;
    private ImageView playImg;
    private ImageView nextImg;
    private ImageView shuffleImg;
    private MediaControllerCompat mediaControllerCompat;

    public MusicController(Context context) {
        this(context,null);
    }

    public MusicController(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MusicController(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.player_controller_bottom,this,true);
        initView();
    }

    private void initView() {
        repeatImg = (ImageView) findViewById(R.id.iv_media_repeat);
        previousImg = (ImageView) findViewById(R.id.iv_media_previous);
        playImg = (ImageView) findViewById(R.id.iv_media_play);
        nextImg = (ImageView) findViewById(R.id.iv_media_next);
        shuffleImg = (ImageView) findViewById(R.id.iv_media_shuffle);
    }

    public void setMediaControllerCompat(MediaControllerCompat mediaControllerCompat) {
        this.mediaControllerCompat = mediaControllerCompat;
    }

    private void initEvent() {
        playImg.setOnClickListener(this);
        previousImg.setOnClickListener(this);
        nextImg.setOnClickListener(this);
        repeatImg.setOnClickListener(this);
        shuffleImg.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_media_repeat:
                if (mediaControllerCompat.getRepeatMode() == 0) {
                    repeatImg.setBackgroundResource(R.drawable.basecopy);
                    mediaControllerCompat.getTransportControls().setRepeatMode(PlaybackStateCompat.REPEAT_MODE_ALL);
                } else {
                    repeatImg.setBackgroundResource(R.drawable.base);
                    mediaControllerCompat.getTransportControls().setRepeatMode(PlaybackStateCompat.REPEAT_MODE_NONE);
                }
                break;
            case R.id.iv_media_shuffle:
                if (mediaControllerCompat.getShuffleMode() == 0) {
                    shuffleImg.setBackgroundResource(R.drawable.basecopy);
                    mediaControllerCompat.getTransportControls().setShuffleMode(PlaybackStateCompat.SHUFFLE_MODE_ALL);
                } else {
                    shuffleImg.setBackgroundResource(R.drawable.base);
                    mediaControllerCompat.getTransportControls().setShuffleMode(PlaybackStateCompat.SHUFFLE_MODE_NONE);
                }
                break;
            case R.id.iv_media_previous:
                mediaControllerCompat.getTransportControls().skipToPrevious();
                break;
            case R.id.iv_media_next:
                mediaControllerCompat.getTransportControls().skipToNext();
                break;
            case R.id.iv_media_play: {
                if (mediaControllerCompat.getPlaybackState().getState() == PlaybackStateCompat.STATE_PLAYING) {
                    mediaControllerCompat.getTransportControls().pause();
                } else {
                    mediaControllerCompat.getTransportControls().play();
                }
                break;
            }
        }
    }

    private void updateUI() {
        //PlayerState代表播放状态
        switch (mediaControllerCompat.getPlaybackState().getState()) {
            case PlaybackStateCompat.STATE_PLAYING:
            case PlaybackStateCompat.STATE_STOPPED:
                playImg.setImageResource(R.drawable.play);
                break;

            case PlaybackStateCompat.STATE_PAUSED:
                playImg.setImageResource(R.drawable.pause);
                break;

        }
        if (mediaControllerCompat.getPlaybackState().getState() == PlaybackStateCompat.STATE_PAUSED) {
            ObjectAnimator.ofFloat(this, "translationY", 0F, 180F).setDuration(0).start();
        }
    }
}
